package com.nebula.salary.portal.config.security.service;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nebula.salary.model.pojo.User;
import com.nebula.salary.model.vo.LoginUser;
import com.nebula.salary.portal.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Spring Security 用户验证处理实现类
 *
 * @author codex
 * @since 2022-02-11
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    
    @Resource
    private IUserService userService;
    
    public static final Log log = LogFactory.get();
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    
        User user = userService.selectUserByUsername(username);
    
        if (user == null) {
            log.info("用户名：{}，不存在", username);
            throw new UsernameNotFoundException("用户名：" + username + "，不存在");
        }
    
        return new LoginUser(user);
    }
}
